﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Oracle.ManagedDataAccess.Client;
using System.Data.SqlClient;
using Aspfm.Main;
using System.Data.SQLite;

namespace Aspfm.Dal
{
    public class DbTool
    {
        public static bool TestConn(ref string errmsg,string connkey)
        {
            if (string.IsNullOrWhiteSpace(connkey) || connkey.Trim().ToLower() == "undefined")
            {
                connkey = "Default";
            }
            DataSourceList dslist = ConfigTool.ReadConn();
            if (dslist != null)
            {
                foreach (DataSource ds in dslist.dataList)
                {
                    if (ds.Key.ToLower() == connkey.ToLower())
                    {
                        return TestConn(ref errmsg, ds);
                    }
                }
            }
            errmsg = "没有找到数据库连接：" + connkey;
            return false;
        }
        public static bool TestConn(ref string errmsg, DataSource dataconn)
        {
            if (dataconn != null)
            {
                return TestConn(ref errmsg,dataconn.datatype, dataconn.ConnStr);
            }
            return false;
        }
        public static bool TestConn(ref string errmsg, DataSourceType datatype, string ConnStr)
        {
            try
            {
                if (datatype == DataSourceType.Oracle)
                {
                    using (OracleConnection oracleConn = new OracleConnection(ConnStr))
                    {
                        oracleConn.Open();
                        oracleConn.Close();
                        return true;
                    }
                }
                else if (datatype == DataSourceType.MSSql)
                {
                    using (SqlConnection sqlConn = new SqlConnection(ConnStr))
                    {
                        sqlConn.Open();
                        sqlConn.Close();
                        return true;
                    }
                }
                else if (datatype == DataSourceType.SQLite)
                {
                    using (SQLiteConnection sqlConn = new SQLiteConnection(ConnStr))
                    {
                        sqlConn.Open();
                        sqlConn.Close();
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                errmsg = ex.Message;
            }
            return false;
        }
        public static void InitConn()
        {
            DataSourceList dslist = ConfigTool.ReadConn();
            if (dslist != null)
            {
                foreach (DataSource ds in dslist.dataList)
                {
                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(TestConnThread), ds);
                }
            }
        }
        public static void TestConnThread(object connds)
        {
            if (connds != null && connds is DataSource)
            {
                string errmsg = "";
                DataSource dataconn = (DataSource)connds;
                TestConn(ref errmsg, dataconn.datatype, dataconn.ConnStr);
            }
        }
    }
}
